//
//  ImageWallViewController.m
//  HengHuaSupervision
//
//  Created by wanglong8889@126.com on 14-3-21.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "ImageWallViewController.h"
#import "XDTools.h"
#import "XDHeader.h"
#import "PhotoDetailViewController.h"
#import "DetailsViewController.h"
#import "photoWallCell.h"
#import "PhotoWall.h"
@interface ImageWallViewController ()

@end

@implementation ImageWallViewController

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}
-(id)initWithAsserId:(NSString *)asserId{
    self = [super init];
    if (self) {
        self.assertId = asserId;
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self calculateRemindBtnFrame:[XDTools sharedXDTools].waitCumitCount];
    [self calculateRemindBtnFrame:[[NSUserDefaults standardUserDefaults] valueForKey:@"waitCunitCount"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [XDTools sharedXDTools].ivc = self;
    
    dataArrary = [NSMutableArray arrayWithCapacity:3];
    bgUrlArrary = [NSMutableArray arrayWithCapacity:3];
    [self initWall];
    [self getWallDatas];
    
}
-(void)initWall{
    
    UIImageView *topView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 46)];
    topView.image = [[UIImage imageNamed:@"detail_info-label"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, .3, 10, .3)];
    
    

    detailLabel = [UILabel initLabel:CGRectMake(21, 0, 100, mLabelEdit_Head_Img.size.height) textFont:mHomepage_text_Font textColor:mLabelEdit_Title_Color textAlignment:NSTextAlignmentLeft];
    detailLabel.text = @"设备照片";
    [topView addSubview:detailLabel];
    [self.contentView addSubview:topView];
    
//    
//    for (int i=0; i<4; i++) {
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToPhotos:)];
//        
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+(225+10)*i,topView.frame.origin.y+topView.frame.size.height+17 , 225, 161)];
//        
//        imageView.tag = 1000+i;
//        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i+1]];
//        imageView.userInteractionEnabled = YES;
//        [imageView addGestureRecognizer:tap];
//        [dataArrary addObject:imageView.image];
//        
//        [self.contentView addSubview:imageView];
//    }
    
 
    wallTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, topView.frame.origin.y+topView.frame.size.height+17, 930, 645) style:UITableViewStylePlain];
    wallTableView.delegate = self;
    wallTableView.dataSource = self;
    wallTableView.backgroundColor = [UIColor clearColor];
    wallTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.contentView addSubview:wallTableView];
}
//获取照片墙数据
-(void)getWallDatas{
    [dataArrary removeAllObjects];
    [bgUrlArrary removeAllObjects];
    FMDatabase * db = [XDTools getDb];
    if (![db open]){
        DDLOG(@"数据打开失败");
        return;
    }
    FMResultSet * rs = [db executeQuery:@"SELECT * FROM T_TRACE_SCAN_LOG where assert_id = ?",_assertId];
    while ([rs next]) {
        PhotoWall *wall = [[PhotoWall alloc] init];
        wall.bgUrlString = [rs objectForColumnName:@"photphoto"];
        wall.state = [rs objectForColumnName:@"status"];
        wall.time = [rs objectForColumnName:@"scan_time"];
        wall.urlString = [rs objectForColumnName:@"breviaryphoto"];
        wall.tag_code = [rs objectForColumnName:@"tag_code"];
        FMResultSet *rs2 = [db executeQuery:@"SELECT * FROM T_TRACE_ASSERTS_TAG where assert_id = ? and tag_code = ?",_assertId,wall.tag_code];
        while ([rs2 next]) {
            wall.location = [rs2 objectForColumnName:@"part"];
        }
       
        [self handleWallDatas:wall.urlString wall:wall];
        [self saveBgUrl:wall.bgUrlString];
    }
    [db close];
    detailLabel.text = [NSString stringWithFormat:@"设备照片(%d)",[dataArrary count]];
    [wallTableView reloadData];
}
-(void)handleWallDatas:(NSString *)urlS wall:(PhotoWall *)w{
    if ([urlS length] == 0) {
//        urlS = @"";
//        PhotoWall *wall = [[PhotoWall alloc] initUrlString:urlS time:w.time location:w.location state:w.state];
//        wall.tag_code = w.tag_code;
//        [dataArrary addObject:wall];
//        DDLOG(@"---------------%@,%@,%@,%@,%@",wall.urlString,wall.time,wall.state,wall.location,wall.tag_code);
    }else{
        NSArray *arrary = [urlS componentsSeparatedByString:@","];
        for (int i = 0; i<[arrary count]; i++) {
            PhotoWall *wall = [[PhotoWall alloc] initUrlString:[arrary objectAtIndex:i] time:w.time location:w.location state:w.state];
            wall.tag_code = w.tag_code;
            [dataArrary addObject:wall];
             DDLOG(@"---------------%@,%@,%@,%@,%@",wall.urlString,wall.time,wall.state,wall.location,wall.tag_code);
            
        }
        [wallTableView reloadData];
    }
//    PhotoWall *wall = [p]
}
//存储大图的url
-(void)saveBgUrl:(NSString *)urlStr{
    if ([urlStr length] == 0) {
//        urlStr = @"";
//        [bgUrlArrary addObject:urlStr];
    }else{
        NSArray *arrary = [urlStr componentsSeparatedByString:@","];
        [bgUrlArrary addObjectsFromArray:arrary];
    }
}


#pragma UItableView  delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([dataArrary count]%4 == 0) {
        rowNum = [dataArrary count]/4;
    }else{
         rowNum = [dataArrary count]/4+1;
    }
   
    return rowNum;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifer = @"wallCell";
    photoWallCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell) {
        cell = [[photoWallCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.delegate = self;
    }
    int yushu = [dataArrary count]%4;
    if (yushu>0&&yushu<4) {
        if (indexPath.row == rowNum-1) {
            for (int i = yushu+1; i<5; i++) {
               // pWallView *wView = (pWallView *)[cell.contentView viewWithTag:100+i];
                if(i == 1){
                    cell.pFirstView.hidden =YES;
                }else if (i ==2){
                    cell.pSecondView.hidden =YES;
                }else if (i == 3){
                    cell.pThirdView.hidden = YES;
                }else if (i == 4){
                    cell.pFourthView.hidden = YES;
                }
            }
        }else{
            for (pWallView *v in cell.contentView.subviews) {
                v.hidden = NO;
            }
        }
    }
    cell.pFirstView.pWall = [dataArrary objectAtIndex:indexPath.row*4];
    if ([dataArrary count]>(indexPath.row*4+1)) {
        cell.pSecondView.pWall = [dataArrary objectAtIndex:indexPath.row*4+1];
    }
    if ([dataArrary count]>(indexPath.row*4+2)) {
        cell.pThirdView.pWall = [dataArrary objectAtIndex:indexPath.row*4+2];
    }
    if ([dataArrary count]>(indexPath.row*4+3)) {
        cell.pFourthView.pWall = [dataArrary objectAtIndex:indexPath.row*4+3];
    }
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 216.f;
}

//实现代理方法
-(void)goPhotos:(PhotoWall *)wall{
    PhotoDetailViewController *photoVC = [[PhotoDetailViewController alloc] init];
    photoVC.myArray = bgUrlArrary;
    int offset = [dataArrary indexOfObject:wall];
    photoVC.contentOffset = offset;
    photoVC.deVC = self.detailVC;
    photoVC.isDefualt = NO;
    [self.detailVC.navigationController pushViewController:photoVC animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
